package com.caigou.modules.channel.ui

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.modules.channel.R
import com.caigou.modules.channel.adapter.BlackHouseItemAdapter
import com.caigou.modules.channel.bean.BannedBean
import com.caigou.modules.channel.bean.BannedItemBean
import com.caigou.modules.channel.databinding.FragmentBlackHouseBinding
import com.caigou.modules.channel.viewmodel.ChannelViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/10/14
 */
@AndroidEntryPoint
class BlackHouseFragment : BaseFragment() {

    private var _binding: FragmentBlackHouseBinding? = null
    private val binding get() = _binding!!

    private val channelViewModel: ChannelViewModel by viewModels()

    private val bannedList = mutableListOf<BannedItemBean>()

    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(requireActivity())
    }

    private val mAdapter: BlackHouseItemAdapter by lazy {
        BlackHouseItemAdapter(bannedList)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentBlackHouseBinding.inflate(inflater, container, false)
        return _binding?.root
    }


    override fun initView() {
        binding.blackHouseSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                channelViewModel.getBlackHouseInfo(AppBackgroundInfo.instance.userBean!!.userId)
            }
        }

        binding.blackHouseRecyclerViewList.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
        }

        binding.blackHouseTvBack.setOnClickListener {
            (requireActivity() as ChannelActivity).onBackPressed()
        }
    }

    override fun lazyLoad() {
        channelViewModel.getBlackHouseInfo(AppBackgroundInfo.instance.userBean!!.userId)
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(channelViewModel.blackHouseLiveData, ::handleGetBlackHouseInfo)
    }

    override fun onBack() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun handleGetBlackHouseInfo(status: Resource<BannedBean>) {
        when (status) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.blackHouseSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(status.errorMsg)) {
                    showToast(status.errorMsg)
                } else {
                    showToast("获取小黑屋信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.blackHouseSwipeRefreshLayout.isRefreshing = false

                if (status.data == null) {
                    return
                }
                mAdapter.setList(status.data!!.bannedList.list)

            }
        }
    }


}